package xc.demo;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class LoginUtils {

    // 生成公钥和私钥
    public static void keyPairGenerator() throws NoSuchAlgorithmException {
        // 创建 KeyPairGenerator 实例，并指定算法为 RSA
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        // 初始化密钥大小为 2048 位
        keyPairGenerator.initialize(2048);
        // 生成密钥对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        // 获取公钥和私钥
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // 将公钥和私钥编码为Base64字符串
        String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
        String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
        // 输出公钥和私钥
        System.out.println("Public Key:" + publicKeyString);
        System.out.println("Private Key:" + privateKeyString);
    }

    public static String decrypt(String encryptedPassword, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decodedPassword = Base64.getDecoder().decode(encryptedPassword);
        byte[] decryptedPassword = cipher.doFinal(decodedPassword);
        return new String(decryptedPassword);
    }

    // 从 Base64 字符串加载私钥
    public static PrivateKey loadPrivateKey(String base64PrivateKey) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }


    public static void main(String[] args) throws Exception {
        // 前端传的加密密码
        String encryptedPassword = "G6O1VCt9500KV+W5Zc4EjG/Iv6wNs/67rqlBX7iK6YuPkOqG7F7tWCNMmNXV0n0Q91smjrfQwWQHAjPfK1De61eSJLUk/7RYGZpZ1ng3gZ3gcSSXIV7L72Ek1984zSgcxhUPuGKweQudCQZZiq5xcl1wZz5l2rCuA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OgqW5iwddQAfJpKeE66ZR43G8ccQlLURa1BQP0o3uo+a+tV7BA68f4IeGreX2srjcCgYEA0boQKZ1q2MahHCuJnFQWScb+4AHGrEJVUds3+cItaR7plPLijEH6+jqDrcvUV98tV+rtZNY5F5KgIMz5Tnki06Ni5fZhbNGpMXQVHnCQTjVPOMIEBHlSkv/Ai9xlgk/UGYkvJNdIMQLwwwkLe65CKwwAVtxUA/WwNtI2geCFirsCgYEA0MsjoL3I6HGK96UhZarDnesoz6JO68x2YFPMtLx9OGIlu4wJxLMKGH4RatPdMPQfIzwTaMI66p+2uf0VzOVsAR9wD0tTGlLpPA9E8mVYVLhrNz4KzKFyyECDId09b1UdG+xJU/qKpuvgWxjJzGJyc6LLw9Idu3zdjLtJQwpro58CgYEArCuL4y3orgb58e9H6/8XwP9M80Zh+74E3ssRO8cNknOfG/j8xFnSGKP6v61KJ8z+I2On+RToD5L+Q48hxysozQcfRFusBTJlLET4pWGRBbM70wNrWuTiRe0Q+4A2EPFJ1KDLA/p7uEiKsAH33FPk3APhL212xz1lqMuPlbVky8sCgYEAoBOVF9hY09fuAYVN2WX8iMOO6GUszMuTQ9RvGGuxu1a8zIdjAWaMcTf0lnQWm08PAnPcJ216DSzESaQ9qE/MoBaDUCsEZXbmNc44NTKuAMlI9SmwsbldI3i5BjsTxJuiK0U8SWWgXOZKyLEYS9R/KcNeY40UC+O/VTxbB0F/hkQ=");
        String decryptedPassword = decrypt(encryptedPassword, privateKey);
        System.out.println(decryptedPassword);
    }


    /**  前端
         1、import JSEncrypt from 'jsencrypt'  导入jsencrypt
         2、const encryptor = new JSEncrypt()
         3、encryptor.setPublicKey('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy1UDnAc3ZQ0I/wVVbZmoORrvcs+ZDKDmqUNuRrLO2YFUxPlY3dlAhXFYKRORJ9Mh9TPohhfekWTNpLZ8/9vS2Z7iWllGArseQmxwMs9DctZrcAyQrEGJeIvKhXL91jpwdP2usM+f/gBJwFMlid1G9ned+wlRrxaDCn0XWTkuLddIgGL5sfroOwSPrAfCAMbK0g4RNjwwcBpwLiys0D7Zj2oAWcUsJT/JUXbqzp6xERlkq/ZQD/9FKc5vrRTN0RB6ospE5YVDwaPusaF1Eae9HF6ZVbI3jBNFambeGJMDdvX94gdIlI2Kb3wjMxStNm7vGsoW8D672r4OPoZbhZ/oLQIDAQAB')
         password: encryptor.encrypt(password)
     */

}
